package com.example.mvvm.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.example.mvvm.BR;
import com.example.mvvm.bean.Account;
import com.example.mvvm.databinding.ActivityDemoBinding;
import com.example.mvvm.model.MVVMModel;
import com.example.mvvm.util.MCallBack;


//一般需要传入Application对象，方便在ViewModel中使用application
public class MVVMViewModel extends BaseObservable {
    private MVVMModel mvvmModel;
    private ActivityDemoBinding binding;
    private String result;

    public MVVMViewModel(Application application){
        mvvmModel = new MVVMModel();
    }

    public MVVMViewModel(Application application, ActivityDemoBinding binding){
        mvvmModel = new MVVMModel();
        this.binding = binding;
    }

    public void getData(View view){
        String userInput = binding.etAccount.getText().toString();
        mvvmModel.getAccountData(userInput, new MCallBack() {
            @Override
            public void onSuccess(Account account) {
                String info = account.getName() + "|" +account.getLevel();
                setResult(info);
            }

            @Override
            public void onFailed() {
                setResult("获取数据失败");
            }
        });
    }

    @Bindable
    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
        notifyPropertyChanged(BR.result);
    }
}
